<?php

/**
 * 希望一个局部变量能够在函数调用之间保留它的值
 */

// 可以用 static

// static 变量可以在函数调用之间保留变量值，不过只是在一次脚本调用期间有效
function check_the_count($pitch){
    static $strikes = 0;
    static $balls = 0;

    switch ($pitch){
        case 'foul':
            if(2 == $strikes) break;
        case 'strike':
            $strikes++;
            break;
        case 'ball':
            $balls++;
            break;
    }

    if(3 == $strikes){
        $strikes = $balls = 0;
        return 'strike out';
    }

    if(4 == $balls){
        $strikes = $balls = 0;
        return 'walk';
    }
    return 'at bat';

}

$pitches = ['strike','ball','ball','strike','foul','strike','foul','ball','ball','ball','ball'];
$what_happened = [];

foreach ($pitches as $pitch){
    $what_happened[]=check_the_count($pitch);
}

var_dump($what_happened);
